{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "对AQI的分组结果：\n",
      "             日期    AQI      等级   季度\n",
      "0    2014-01-01   81.0     二级良  一季度\n",
      "1    2014-01-02  145.0  三级轻度污染  一季度\n",
      "2    2014-01-03   74.0     二级良  一季度\n",
      "3    2014-01-04  149.0  三级轻度污染  一季度\n",
      "4    2014-01-05  119.0  三级轻度污染  一季度\n",
      "...         ...    ...     ...  ...\n",
      "2150 2019-11-22  183.0  四级中度污染  四季度\n",
      "2151 2019-11-23  175.0  四级中度污染  四季度\n",
      "2152 2019-11-24   30.0     一级优  四季度\n",
      "2153 2019-11-25   40.0     一级优  四季度\n",
      "2154 2019-11-26   73.0     二级良  四季度\n",
      "\n",
      "[2155 rows x 4 columns]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from pandas import Series,DataFrame\n",
    "\n",
    "data=pd.read_excel('北京市空气质量数据.xlsx')\n",
    "data=data.replace(0,np.NaN)\n",
    "data['年']=data['日期'].apply(lambda x:x.year)\n",
    "month=data['日期'].apply(lambda x:x.month)\n",
    "quarter_month={'1':'一季度','2':'一季度','3':'一季度',\n",
    "               '4':'二季度','5':'二季度','6':'二季度',\n",
    "               '7':'三季度','8':'三季度','9':'三季度',\n",
    "              '10':'四季度','11':'四季度','12':'四季度'}\n",
    "data['季度']=month.map(lambda x:quarter_month[str(x)])\n",
    "bins=[0,50,100,150,200,300,1000]\n",
    "data['等级']=pd.cut(data['AQI'],bins,labels=['一级优','二级良','三级轻度污染','四级中度污染','五级重度污染','六级严重污染'])\n",
    "print('对AQI的分组结果：\\n{0}'.format(data[['日期','AQI','等级','季度']]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:1: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working\n",
      "  \"\"\"Entry point for launching an IPython kernel.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from collections import Iterable\n",
    "isinstance(month,Iterable)   #判断month是否为可迭代的对象\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "代码说明：\n",
    "（1）第6行：利用数据框函数replace()将数据框中的0（表示无监测结果）替换为缺失值NaN。\n",
    "（2）第7，8行：利用.apply()方法以及匿名函数，基于“日期”变量得到每个样本观测的年份和月份。\n",
    "（3）第9行：建立一个关于月份和季度的字典quarter_month。\n",
    "（4）第10行：利用Python函数map()，依据字典quarter_month，将序列month中的1，2，3等月份映射（对应）到相应的季度上。\n",
    "（5）第14行：生成一个后续用于对AQI分组的列表bins。它描述了AQI和空气质量等级的数值对应关系。\n",
    "（6）第15行：利用Pandas的cut()方法对AQI进行分组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "各季度AQI和PM2.5的均值:\n",
      "            AQI      PM2.5\n",
      "季度                        \n",
      "一季度  109.327778  77.225926\n",
      "三季度   98.911071  49.528131\n",
      "二季度  109.369004  55.149723\n",
      "四季度  109.612403  77.195736\n",
      "各季度AQI和PM2.5的描述统计量:\n",
      "                   AQI       PM2.5\n",
      "季度                               \n",
      "一季度 count  540.000000  540.000000\n",
      "    mean   109.327778   77.225926\n",
      "    std     80.405408   73.133857\n",
      "    min     26.000000    4.000000\n",
      "    25%     48.000000   24.000000\n",
      "    50%     80.000000   53.000000\n",
      "    75%    145.000000  109.250000\n",
      "    max    470.000000  454.000000\n",
      "三季度 count  551.000000  551.000000\n",
      "    mean    98.911071   49.528131\n",
      "    std     45.484516   35.394897\n",
      "    min     28.000000    3.000000\n",
      "    25%     60.000000   23.000000\n",
      "    50%     95.000000   41.000000\n",
      "    75%    130.500000   67.000000\n",
      "    max    252.000000  202.000000\n",
      "二季度 count  542.000000  541.000000\n",
      "    mean   109.369004   55.149723\n",
      "    std     49.608042   35.918345\n",
      "    min     35.000000    5.000000\n",
      "    25%     71.000000   27.000000\n",
      "    50%     99.000000   47.000000\n",
      "    75%    140.750000   73.000000\n",
      "    max    500.000000  229.000000\n",
      "四季度 count  516.000000  516.000000\n",
      "    mean   109.612403   77.195736\n",
      "    std     84.192134   76.651794\n",
      "    min     21.000000    4.000000\n",
      "    25%     55.000000   25.000000\n",
      "    50%     78.000000   51.000000\n",
      "    75%    137.250000  101.500000\n",
      "    max    485.000000  477.000000\n",
      "空气质量最差的5天:\n",
      "              日期    AQI  PM2.5      等级\n",
      "1218 2017-05-04  500.0    NaN  六级严重污染\n",
      "723  2015-12-25  485.0  477.0  六级严重污染\n",
      "699  2015-12-01  476.0  464.0  六级严重污染\n",
      "1095 2017-01-01  470.0  454.0  六级严重污染\n",
      "698  2015-11-30  450.0  343.0  六级严重污染\n",
      "各季度空气质量最差的3天:\n",
      "                  日期    AQI  PM2.5      等级\n",
      "季度                                       \n",
      "一季度 1095 2017-01-01  470.0  454.0  六级严重污染\n",
      "    45   2014-02-15  428.0  393.0  六级严重污染\n",
      "    55   2014-02-25  403.0  354.0  六级严重污染\n",
      "三季度 186  2014-07-06  252.0  202.0  五级重度污染\n",
      "    211  2014-07-31  245.0  195.0  五级重度污染\n",
      "    183  2014-07-03  240.0  190.0  五级重度污染\n",
      "二季度 1218 2017-05-04  500.0    NaN  六级严重污染\n",
      "    1219 2017-05-05  342.0  181.0  六级严重污染\n",
      "    103  2014-04-14  279.0  229.0  五级重度污染\n",
      "四季度 723  2015-12-25  485.0  477.0  六级严重污染\n",
      "    699  2015-12-01  476.0  464.0  六级严重污染\n",
      "    698  2015-11-30  450.0  343.0  六级严重污染\n",
      "各季度空气质量情况:\n",
      " 季度      一季度  三季度  二季度  四季度    总计\n",
      "等级                              \n",
      "一级优     145   96   38  108   387\n",
      "二级良     170  209  240  230   849\n",
      "三级轻度污染   99  164  152   64   479\n",
      "四级中度污染   57   72   96   33   258\n",
      "五级重度污染   48   10   14   58   130\n",
      "六级严重污染   21    0    2   23    46\n",
      "总计      540  551  542  516  2149\n"
     ]
    }
   ],
   "source": [
    "print('各季度AQI和PM2.5的均值:\\n{0}'.format(data.loc[:,['AQI','PM2.5']].groupby(data['季度']).mean()))\n",
    "print('各季度AQI和PM2.5的描述统计量:\\n',data.groupby(data['季度'])['AQI','PM2.5'].apply(lambda x:x.describe()))\n",
    "\n",
    "def top(df,n=10,column='AQI'):\n",
    "    return df.sort_values(by=column,ascending=False)[:n]\n",
    "print('空气质量最差的5天:\\n',top(data,n=5)[['日期','AQI','PM2.5','等级']])\n",
    "print('各季度空气质量最差的3天:\\n',data.groupby(data['季度']).apply(lambda x:top(x,n=3)[['日期','AQI','PM2.5','等级']]))\n",
    "print('各季度空气质量情况:\\n',pd.crosstab(data['等级'],data['季度'],margins=True,margins_name='总计',normalize=False))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "代码说明：\n",
    "（1）第1行：利用数据框的groupby()方法，计算各季度AQI和PM2.5的平均值。\n",
    "（2）第2行：计算几个季度AQI和PM2.5的基本描述统计量（均值，标准差，最小值，四分位数，最大值）。\n",
    "（3）第4，5行：定义了一个名为top的用户自定义函数：对给定数据框，按指定列（默认AQI列）值的降序排序，返回排在前n（默认10）条数据。\n",
    "（4）第6行：调用用户自定义函数top，对data数据框中，按AQI值的降序排序并返回前5条数据，即AQI最高的5天的数据。\n",
    "（5）第7行：首先对数据按季度分组，依次对分组数据调用用户自定义函数top，得到各季度AQI最高的3天数据。\n",
    "（6）第8行：利用Pandas函数crosstab()对数据按季度和空气质量等级交叉分组，并给出各个组的样本量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>AQI</th>\n",
       "      <th>质量等级</th>\n",
       "      <th>PM2.5</th>\n",
       "      <th>PM10</th>\n",
       "      <th>SO2</th>\n",
       "      <th>CO</th>\n",
       "      <th>NO2</th>\n",
       "      <th>O3</th>\n",
       "      <th>年</th>\n",
       "      <th>季度</th>\n",
       "      <th>等级</th>\n",
       "      <th>一级优</th>\n",
       "      <th>二级良</th>\n",
       "      <th>三级轻度污染</th>\n",
       "      <th>四级中度污染</th>\n",
       "      <th>五级重度污染</th>\n",
       "      <th>六级严重污染</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>2014-01-01</td>\n",
       "      <td>81.0</td>\n",
       "      <td>良</td>\n",
       "      <td>45.0</td>\n",
       "      <td>111.0</td>\n",
       "      <td>28.0</td>\n",
       "      <td>1.5</td>\n",
       "      <td>62.0</td>\n",
       "      <td>52.0</td>\n",
       "      <td>2014</td>\n",
       "      <td>一季度</td>\n",
       "      <td>二级良</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>2014-01-02</td>\n",
       "      <td>145.0</td>\n",
       "      <td>轻度污染</td>\n",
       "      <td>111.0</td>\n",
       "      <td>168.0</td>\n",
       "      <td>69.0</td>\n",
       "      <td>3.4</td>\n",
       "      <td>93.0</td>\n",
       "      <td>14.0</td>\n",
       "      <td>2014</td>\n",
       "      <td>一季度</td>\n",
       "      <td>三级轻度污染</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>2014-01-03</td>\n",
       "      <td>74.0</td>\n",
       "      <td>良</td>\n",
       "      <td>47.0</td>\n",
       "      <td>98.0</td>\n",
       "      <td>29.0</td>\n",
       "      <td>1.3</td>\n",
       "      <td>52.0</td>\n",
       "      <td>56.0</td>\n",
       "      <td>2014</td>\n",
       "      <td>一季度</td>\n",
       "      <td>二级良</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>2014-01-04</td>\n",
       "      <td>149.0</td>\n",
       "      <td>轻度污染</td>\n",
       "      <td>114.0</td>\n",
       "      <td>147.0</td>\n",
       "      <td>40.0</td>\n",
       "      <td>2.8</td>\n",
       "      <td>75.0</td>\n",
       "      <td>14.0</td>\n",
       "      <td>2014</td>\n",
       "      <td>一季度</td>\n",
       "      <td>三级轻度污染</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>2014-01-05</td>\n",
       "      <td>119.0</td>\n",
       "      <td>轻度污染</td>\n",
       "      <td>91.0</td>\n",
       "      <td>117.0</td>\n",
       "      <td>36.0</td>\n",
       "      <td>2.3</td>\n",
       "      <td>67.0</td>\n",
       "      <td>44.0</td>\n",
       "      <td>2014</td>\n",
       "      <td>一季度</td>\n",
       "      <td>三级轻度污染</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2150</td>\n",
       "      <td>2019-11-22</td>\n",
       "      <td>183.0</td>\n",
       "      <td>中度污染</td>\n",
       "      <td>138.0</td>\n",
       "      <td>181.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>2.4</td>\n",
       "      <td>94.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>2019</td>\n",
       "      <td>四季度</td>\n",
       "      <td>四级中度污染</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2151</td>\n",
       "      <td>2019-11-23</td>\n",
       "      <td>175.0</td>\n",
       "      <td>中度污染</td>\n",
       "      <td>132.0</td>\n",
       "      <td>137.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>1.6</td>\n",
       "      <td>69.0</td>\n",
       "      <td>34.0</td>\n",
       "      <td>2019</td>\n",
       "      <td>四季度</td>\n",
       "      <td>四级中度污染</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2152</td>\n",
       "      <td>2019-11-24</td>\n",
       "      <td>30.0</td>\n",
       "      <td>优</td>\n",
       "      <td>7.0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>0.2</td>\n",
       "      <td>11.0</td>\n",
       "      <td>58.0</td>\n",
       "      <td>2019</td>\n",
       "      <td>四季度</td>\n",
       "      <td>一级优</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2153</td>\n",
       "      <td>2019-11-25</td>\n",
       "      <td>40.0</td>\n",
       "      <td>优</td>\n",
       "      <td>13.0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>0.4</td>\n",
       "      <td>32.0</td>\n",
       "      <td>29.0</td>\n",
       "      <td>2019</td>\n",
       "      <td>四季度</td>\n",
       "      <td>一级优</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2154</td>\n",
       "      <td>2019-11-26</td>\n",
       "      <td>73.0</td>\n",
       "      <td>良</td>\n",
       "      <td>38.0</td>\n",
       "      <td>72.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.8</td>\n",
       "      <td>58.0</td>\n",
       "      <td>14.0</td>\n",
       "      <td>2019</td>\n",
       "      <td>四季度</td>\n",
       "      <td>二级良</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2155 rows × 18 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             日期    AQI  质量等级  PM2.5   PM10   SO2   CO   NO2    O3     年   季度  \\\n",
       "0    2014-01-01   81.0     良   45.0  111.0  28.0  1.5  62.0  52.0  2014  一季度   \n",
       "1    2014-01-02  145.0  轻度污染  111.0  168.0  69.0  3.4  93.0  14.0  2014  一季度   \n",
       "2    2014-01-03   74.0     良   47.0   98.0  29.0  1.3  52.0  56.0  2014  一季度   \n",
       "3    2014-01-04  149.0  轻度污染  114.0  147.0  40.0  2.8  75.0  14.0  2014  一季度   \n",
       "4    2014-01-05  119.0  轻度污染   91.0  117.0  36.0  2.3  67.0  44.0  2014  一季度   \n",
       "...         ...    ...   ...    ...    ...   ...  ...   ...   ...   ...  ...   \n",
       "2150 2019-11-22  183.0  中度污染  138.0  181.0   9.0  2.4  94.0   5.0  2019  四季度   \n",
       "2151 2019-11-23  175.0  中度污染  132.0  137.0   6.0  1.6  69.0  34.0  2019  四季度   \n",
       "2152 2019-11-24   30.0     优    7.0   30.0   3.0  0.2  11.0  58.0  2019  四季度   \n",
       "2153 2019-11-25   40.0     优   13.0   30.0   3.0  0.4  32.0  29.0  2019  四季度   \n",
       "2154 2019-11-26   73.0     良   38.0   72.0   6.0  0.8  58.0  14.0  2019  四季度   \n",
       "\n",
       "          等级  一级优  二级良  三级轻度污染  四级中度污染  五级重度污染  六级严重污染  \n",
       "0        二级良    0    1       0       0       0       0  \n",
       "1     三级轻度污染    0    0       1       0       0       0  \n",
       "2        二级良    0    1       0       0       0       0  \n",
       "3     三级轻度污染    0    0       1       0       0       0  \n",
       "4     三级轻度污染    0    0       1       0       0       0  \n",
       "...      ...  ...  ...     ...     ...     ...     ...  \n",
       "2150  四级中度污染    0    0       0       1       0       0  \n",
       "2151  四级中度污染    0    0       0       1       0       0  \n",
       "2152     一级优    1    0       0       0       0       0  \n",
       "2153     一级优    1    0       0       0       0       0  \n",
       "2154     二级良    0    1       0       0       0       0  \n",
       "\n",
       "[2155 rows x 18 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.get_dummies(data['等级'])\n",
    "data.join(pd.get_dummies(data['等级']))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "代码说明：\n",
    "（1）第1行：利用Pandas的get_dummies得到分类型变量“等级”的哑变量。\n",
    "（2）第2行：利用数据框的join()方法，将原始数据和哑变量数据，按行索引进行横向合并。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1346 1122 1766 2154 1147 1593 1761   96   47   73]\n",
      "[1883  326   43 1627 1750 1440  993 1469 1892  865]\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>AQI</th>\n",
       "      <th>质量等级</th>\n",
       "      <th>PM2.5</th>\n",
       "      <th>PM10</th>\n",
       "      <th>SO2</th>\n",
       "      <th>CO</th>\n",
       "      <th>NO2</th>\n",
       "      <th>O3</th>\n",
       "      <th>年</th>\n",
       "      <th>季度</th>\n",
       "      <th>等级</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>7</td>\n",
       "      <td>2014-01-08</td>\n",
       "      <td>27.0</td>\n",
       "      <td>优</td>\n",
       "      <td>15.0</td>\n",
       "      <td>25.0</td>\n",
       "      <td>13.0</td>\n",
       "      <td>0.5</td>\n",
       "      <td>21.0</td>\n",
       "      <td>53.0</td>\n",
       "      <td>2014</td>\n",
       "      <td>一季度</td>\n",
       "      <td>一级优</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>8</td>\n",
       "      <td>2014-01-09</td>\n",
       "      <td>46.0</td>\n",
       "      <td>优</td>\n",
       "      <td>27.0</td>\n",
       "      <td>46.0</td>\n",
       "      <td>19.0</td>\n",
       "      <td>0.8</td>\n",
       "      <td>35.0</td>\n",
       "      <td>53.0</td>\n",
       "      <td>2014</td>\n",
       "      <td>一季度</td>\n",
       "      <td>一级优</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>11</td>\n",
       "      <td>2014-01-12</td>\n",
       "      <td>47.0</td>\n",
       "      <td>优</td>\n",
       "      <td>27.0</td>\n",
       "      <td>47.0</td>\n",
       "      <td>27.0</td>\n",
       "      <td>0.7</td>\n",
       "      <td>39.0</td>\n",
       "      <td>59.0</td>\n",
       "      <td>2014</td>\n",
       "      <td>一季度</td>\n",
       "      <td>一级优</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>19</td>\n",
       "      <td>2014-01-20</td>\n",
       "      <td>35.0</td>\n",
       "      <td>优</td>\n",
       "      <td>8.0</td>\n",
       "      <td>35.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.3</td>\n",
       "      <td>15.0</td>\n",
       "      <td>65.0</td>\n",
       "      <td>2014</td>\n",
       "      <td>一季度</td>\n",
       "      <td>一级优</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>20</td>\n",
       "      <td>2014-01-21</td>\n",
       "      <td>26.0</td>\n",
       "      <td>优</td>\n",
       "      <td>18.0</td>\n",
       "      <td>25.0</td>\n",
       "      <td>27.0</td>\n",
       "      <td>0.7</td>\n",
       "      <td>34.0</td>\n",
       "      <td>50.0</td>\n",
       "      <td>2014</td>\n",
       "      <td>一季度</td>\n",
       "      <td>一级优</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2122</td>\n",
       "      <td>2019-10-25</td>\n",
       "      <td>30.0</td>\n",
       "      <td>优</td>\n",
       "      <td>8.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.4</td>\n",
       "      <td>24.0</td>\n",
       "      <td>55.0</td>\n",
       "      <td>2019</td>\n",
       "      <td>四季度</td>\n",
       "      <td>一级优</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2131</td>\n",
       "      <td>2019-11-03</td>\n",
       "      <td>48.0</td>\n",
       "      <td>优</td>\n",
       "      <td>33.0</td>\n",
       "      <td>48.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>0.6</td>\n",
       "      <td>34.0</td>\n",
       "      <td>33.0</td>\n",
       "      <td>2019</td>\n",
       "      <td>四季度</td>\n",
       "      <td>一级优</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2135</td>\n",
       "      <td>2019-11-07</td>\n",
       "      <td>47.0</td>\n",
       "      <td>优</td>\n",
       "      <td>24.0</td>\n",
       "      <td>47.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>0.5</td>\n",
       "      <td>37.0</td>\n",
       "      <td>44.0</td>\n",
       "      <td>2019</td>\n",
       "      <td>四季度</td>\n",
       "      <td>一级优</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2152</td>\n",
       "      <td>2019-11-24</td>\n",
       "      <td>30.0</td>\n",
       "      <td>优</td>\n",
       "      <td>7.0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>0.2</td>\n",
       "      <td>11.0</td>\n",
       "      <td>58.0</td>\n",
       "      <td>2019</td>\n",
       "      <td>四季度</td>\n",
       "      <td>一级优</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2153</td>\n",
       "      <td>2019-11-25</td>\n",
       "      <td>40.0</td>\n",
       "      <td>优</td>\n",
       "      <td>13.0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>0.4</td>\n",
       "      <td>32.0</td>\n",
       "      <td>29.0</td>\n",
       "      <td>2019</td>\n",
       "      <td>四季度</td>\n",
       "      <td>一级优</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>387 rows × 12 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             日期   AQI 质量等级  PM2.5  PM10   SO2   CO   NO2    O3     年   季度   等级\n",
       "7    2014-01-08  27.0    优   15.0  25.0  13.0  0.5  21.0  53.0  2014  一季度  一级优\n",
       "8    2014-01-09  46.0    优   27.0  46.0  19.0  0.8  35.0  53.0  2014  一季度  一级优\n",
       "11   2014-01-12  47.0    优   27.0  47.0  27.0  0.7  39.0  59.0  2014  一季度  一级优\n",
       "19   2014-01-20  35.0    优    8.0  35.0   6.0  0.3  15.0  65.0  2014  一季度  一级优\n",
       "20   2014-01-21  26.0    优   18.0  25.0  27.0  0.7  34.0  50.0  2014  一季度  一级优\n",
       "...         ...   ...  ...    ...   ...   ...  ...   ...   ...   ...  ...  ...\n",
       "2122 2019-10-25  30.0    优    8.0  20.0   2.0  0.4  24.0  55.0  2019  四季度  一级优\n",
       "2131 2019-11-03  48.0    优   33.0  48.0   3.0  0.6  34.0  33.0  2019  四季度  一级优\n",
       "2135 2019-11-07  47.0    优   24.0  47.0   3.0  0.5  37.0  44.0  2019  四季度  一级优\n",
       "2152 2019-11-24  30.0    优    7.0  30.0   3.0  0.2  11.0  58.0  2019  四季度  一级优\n",
       "2153 2019-11-25  40.0    优   13.0  30.0   3.0  0.4  32.0  29.0  2019  四季度  一级优\n",
       "\n",
       "[387 rows x 12 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.seed(123)\n",
    "sampler=np.random.randint(0,len(data),10)\n",
    "print(sampler)\n",
    "sampler=np.random.permutation(len(data))[:10]\n",
    "print(sampler)\n",
    "\n",
    "data.take(sampler)\n",
    "data.loc[data['质量等级']=='优',:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "代码说明：\n",
    "（1）第2行：利用Pandas函数random.randint()在指定范围内随机抽取指定个数（这里是10）的随机数。\n",
    "（2）第 4行：利用Pandas函数random.permutation是对数据随机打乱重排。之后再抽取前10个样本观测。\n",
    "（3）第7行：利用数据框的take()方法，基于指定随机数获得数据集的一个子集。\n",
    "（4）第8行：利用数据框访问的方式，抽取满足指定条件（质量等级等于优）行的数据。\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
